﻿/*
* Copyright (c) 2010-2012 Tesla Engine Group
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/


using System;
using Tesla.Graphics;
using Tesla.Graphics.Implementation;
using XFG = Microsoft.Xna.Framework.Graphics;

namespace Tesla.Xna.Graphics.Implementation {
    /// <summary>
    /// Concrete XNA 4.0 implementation for <see cref="Effect"/>.
    /// </summary>
    public sealed class XNAEffectImplementation : EffectImplementation {
        private byte[] _cachedByteCode;
        private XNARenderer _renderer;
        private XFG.GraphicsDevice _graphicsDevice;
        private XFG.Effect _effect;
        private XNAEffectTechnique _currentTechnique;

        /// <summary>
        /// Gets the XNA renderer that created this object.
        /// </summary>
        internal XNARenderer Renderer {
            get {
                return _renderer;
            }
        }

        /// <summary>
        /// Gets the actual underlying XNA effect object.
        /// </summary>
        internal XFG.Effect XNAEffect {
            get {
                return _effect;
            }
        }

        /// <summary>
        /// Gets or sets the current technique.
        /// </summary>
        /// <exception cref="System.ArgumentNullException">Thrown if the technique is null.</exception>
        public override IEffectTechnique CurrentTechnique {
            get {
                return _currentTechnique;
            }
            set {

                if(value == null) {
                    throw new ArgumentNullException("Current technique must not be null");
                }

                XNAEffectTechnique tech = (XNAEffectTechnique) value;
                _effect.CurrentTechnique = tech.XNATechnique;
                _currentTechnique = tech;
            }
        }

        /// <summary>
        /// Creates a new instance of <see cref="XNAEffectImplementation"/>.
        /// </summary>
        /// <param name="renderer">The XNA renderer.</param>
        /// <param name="byteCode">The compiled byte code.</param>
        internal XNAEffectImplementation(XNARenderer renderer, byte[] byteCode) {
            _renderer = renderer;
            _graphicsDevice = renderer.GraphicsDevice;
            _cachedByteCode = byteCode;
            _effect = new XFG.Effect(_graphicsDevice, byteCode);
            base.SetParameters(new XNAEffectParameterCollection(_effect.Parameters));
            base.SetTechniques(new XNAEffectTechniqueCollection(_effect.Techniques));
            _currentTechnique = (XNAEffectTechnique) base.Techniques[0];
        }

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="XNAEffectImplementation"/> is reclaimed by garbage collection.
        /// </summary>
        ~XNAEffectImplementation() {
            Dispose(false);
        }

        /// <summary>
        /// Copies this effect, creating a new instance.
        /// </summary>
        /// <returns>Cloned instance of the effect</returns>
        public override EffectImplementation Clone() {
            return new XNAEffectImplementation(_renderer, _cachedByteCode);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected override void Dispose(bool disposing) {
            if(!IsDisposed) {
                if(_effect != null) {
                    _effect.Dispose();
                }
            }
            base.Dispose(disposing);
        }
    }
}
